﻿using System.Collections.Generic;

namespace UniSender
{
    using Expando = Dictionary<string, object>;
    using IExpando = IDictionary<string, object>;

    public class Field
    {
        public virtual long? Id { get; set; }

        public virtual string Name { get; set; }

        public virtual FieldType? Type { get; set; }

        public virtual bool? Visible { get; set; }

        public virtual int? Position { get; set; }

        public Field()
        { }

        public Field(Native.Field native)
        {
            Name = native.name;
            Type = Utility.ParseEnum<FieldType>(native.type);
            Visible = native.is_visible == null ? null : (bool?)(native.is_visible == 1);
            Position = native.view_pos;
        }

        public virtual IExpando Serialize()
        {
            var args = new Expando();
            args["id"] = Id;
            args["name"] = Name;
            args["type"] = Type.Stringify();
            if (Visible != null)
                args["is_visible"] = Visible.Value ? 1 : 0;
            if (Position != null)
                args["view_pos"] = Position.Value;
            return args;
        }
    }
}
